package iit.projects.etaxi.view.validators;

import iit.projects.etaxi.util.JSFUtils;

import java.text.NumberFormat;
import java.text.ParseException;

import javax.faces.application.FacesMessage;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class InputNumberValidator implements Validator, StateHolder{

	@Override
	public void validate(FacesContext arg0, UIComponent uiComponent, Object object)
			throws ValidatorException {
		
		@SuppressWarnings("unused")
		Number number = null;
		NumberFormat numberFormat = NumberFormat.getNumberInstance();
		
		try {
			number = numberFormat.parse((String)object);
		} catch (ParseException e) {
			throw new ValidatorException(JSFUtils.getMessageFromBundle("common.conversion.notANumber", 
					FacesMessage.SEVERITY_ERROR));
		}
	}

	@Override
	public boolean isTransient() {
		return false;
	}

	@Override
	public void restoreState(FacesContext arg0, Object arg1) {
	}

	@Override
	public Object saveState(FacesContext arg0) {
		return null;
	}

	@Override
	public void setTransient(boolean arg0) {
	}

}
